
package com.coolgor.coldot.service.wechat.oauth2.impl;

import com.coolgor.coldot.common.PageCalculator;
import com.coolgor.coldot.entity.wechat.oauth2.OAuth2AccessToken;
import com.coolgor.coldot.dao.wechat.oauth2.OAuth2AccessTokenDao;
import com.coolgor.coldot.dto.wechat.oauth2.OAuth2AccessTokenExecution;
import com.coolgor.coldot.enums.wechat.oauth2.OAuth2AccessTokenOperationStateEnum;
import com.coolgor.coldot.exceptions.wechat.oauth2.OAuth2AccessTokenOperationException;
import com.coolgor.coldot.service.wechat.oauth2.OAuth2AccessTokenService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


@Service
public class OAuth2AccessTokenServiceImpl implements OAuth2AccessTokenService {

	private static Logger log = LoggerFactory.getLogger(OAuth2AccessTokenServiceImpl.class);

	@Autowired
	OAuth2AccessTokenDao oAuth2AccessTokenDao;

	@Override
	public OAuth2AccessTokenExecution getByID(Long recordID) {
		// 空值判断
		if (null == recordID || recordID.equals("")) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL_ID);
		}
		OAuth2AccessToken oAuth2AccessToken = oAuth2AccessTokenDao.queryByID(recordID);
		if (oAuth2AccessToken != null) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.SUCCESS, oAuth2AccessToken);
		} else {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL);
		}
	}

	@Override
	public OAuth2AccessTokenExecution getList(OAuth2AccessToken oAuth2AccessTokenCondition, int pageIndex, int pageSize) {
		// 将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		// 依据查询条件，调用dao层返回相关的查询结果列表
		List<OAuth2AccessToken> list = oAuth2AccessTokenDao.queryList(oAuth2AccessTokenCondition, rowIndex, pageSize);
		// 依据相同的查询条件，返回查询结果总数
		int count = oAuth2AccessTokenDao.queryCount(oAuth2AccessTokenCondition);
		OAuth2AccessTokenExecution result = new OAuth2AccessTokenExecution();
		if (list != null) {
			result.setState(OAuth2AccessTokenOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(OAuth2AccessTokenOperationStateEnum.SUCCESS.getStateInfo());
			result.setOAuth2AccessTokenList(list);
			result.setCount(count);
		} else {
			result.setState(OAuth2AccessTokenOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public OAuth2AccessTokenExecution add(OAuth2AccessToken oAuth2AccessToken) throws OAuth2AccessTokenOperationException {
		// 空值判断
		if (oAuth2AccessToken == null) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行插入数据操作 
			effectedNum = oAuth2AccessTokenDao.insert(oAuth2AccessToken);
			if (effectedNum <= 0) {
				throw new OAuth2AccessTokenOperationException("OAuth2AccessToken信息插入失败");
			}
		} catch (Exception e) {
			throw new OAuth2AccessTokenOperationException("Add oAuth2AccessToken error:" + e.getMessage());
		}
		return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.SUCCESS, oAuth2AccessToken);
	}

	@Override
	@Transactional
	public OAuth2AccessTokenExecution modify(OAuth2AccessToken oAuth2AccessToken) throws OAuth2AccessTokenOperationException {
		// 空值判断
		if (oAuth2AccessToken == null) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行修改更新操作 
			effectedNum = oAuth2AccessTokenDao.update(oAuth2AccessToken);
			if (effectedNum <= 0) {
				throw new OAuth2AccessTokenOperationException("OAuth2AccessToken信息更新失败");
			}
		} catch (Exception e) {
			throw new OAuth2AccessTokenOperationException("Update oAuth2AccessToken error:" + e.getMessage());
		}
		return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.SUCCESS, oAuth2AccessToken);
	}

    @Override
    public OAuth2AccessTokenExecution uptodate(OAuth2AccessToken oAuth2AccessToken) throws OAuth2AccessTokenOperationException {
		// 空值判断
		if ( null == oAuth2AccessToken
			|| null == oAuth2AccessToken.getAppID()
			|| "".equals(oAuth2AccessToken.getAppID())
			|| null == oAuth2AccessToken.getOpenid()
			|| "".equals(oAuth2AccessToken.getOpenid())
		) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL);
		}
		int effectedNum = -1;
		try {
			OAuth2AccessToken condition = new OAuth2AccessToken();
			condition.setAppID(oAuth2AccessToken.getAppID());
			condition.setOpenid(oAuth2AccessToken.getOpenid());
			List<OAuth2AccessToken> list = oAuth2AccessTokenDao.queryList(condition, 0, 1);
			if (null == list || list.size()==0 ) {
				// 执行插入操作
				effectedNum = oAuth2AccessTokenDao.insert(oAuth2AccessToken);
			} else {
				// 执行修改更新操作
				oAuth2AccessToken.setRecordID(list.get(0).getRecordID());
				effectedNum = oAuth2AccessTokenDao.update(oAuth2AccessToken);
			}
			if (effectedNum <= 0) {
				throw new OAuth2AccessTokenOperationException("OAuth2AccessToken up to date error!");
			}
		} catch (Exception e) {
			throw new OAuth2AccessTokenOperationException("Up to date oAuth2AccessToken error:" + e.getMessage());
		}
		return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.SUCCESS, oAuth2AccessToken);
	}

    @Override
	@Transactional
	public OAuth2AccessTokenExecution remove(Long recordID) throws OAuth2AccessTokenOperationException{
		// 空值判断
		if (recordID == null) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = oAuth2AccessTokenDao.delete(recordID);
			if (effectedNum <= 0) {
				throw new OAuth2AccessTokenOperationException("OAuth2AccessToken单条信息删除失败");
			}
		} catch (Exception e) {
			throw new OAuth2AccessTokenOperationException("Delete oAuth2AccessToken error:" + e.getMessage());
		}
		return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.SUCCESS);
	}

	@Override
	@Transactional
	public OAuth2AccessTokenExecution remove(OAuth2AccessToken oAuth2AccessToken) throws OAuth2AccessTokenOperationException{
		// 空值判断
		if (oAuth2AccessToken == null) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = oAuth2AccessTokenDao.delete(oAuth2AccessToken.getRecordID());
			if (effectedNum <= 0) {
				throw new OAuth2AccessTokenOperationException("OAuth2AccessToken单条信息删除失败");
			}
		} catch (Exception e) {
			throw new OAuth2AccessTokenOperationException("Delete oAuth2AccessToken error:" + e.getMessage());
		}
		return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.SUCCESS, oAuth2AccessToken);
	}

	@Override
	@Transactional
	public OAuth2AccessTokenExecution removeBatchByID(List<Long> recordIDList) throws OAuth2AccessTokenOperationException{
		// 空值判断
		if (recordIDList == null || recordIDList.size() < 1) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = oAuth2AccessTokenDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new OAuth2AccessTokenOperationException("OAuth2AccessToken多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new OAuth2AccessTokenOperationException("Batch Delete oAuth2AccessToken error:" + e.getMessage());
		}
		OAuth2AccessTokenExecution exe = new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	@Transactional
	public OAuth2AccessTokenExecution removeBatch(List<OAuth2AccessToken> oAuth2AccessTokenList) throws OAuth2AccessTokenOperationException{
		// 空值判断
		if (oAuth2AccessTokenList == null) {
			return new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.NULL);
		} 
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<oAuth2AccessTokenList.size(); i++) {
			recordIDList.add(oAuth2AccessTokenList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = oAuth2AccessTokenDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new OAuth2AccessTokenOperationException("OAuth2AccessToken多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new OAuth2AccessTokenOperationException("Batch Delete oAuth2AccessToken error:" + e.getMessage());
		}
		OAuth2AccessTokenExecution exe = new OAuth2AccessTokenExecution(OAuth2AccessTokenOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

}
